#include "rtsh.h"

namespace rtsh {

rtsh::rtsh (const char *program, const char *prompt) :
   __MaxHistory (100)
{
//   __error = error::error (program);

   __hist = history_init();
   if (!__hist)
      printf("%s history_init()", __func__);

   history(__hist, &__histevent, H_SETSIZE, __MaxHistory);
   history(__hist, &__histevent, H_SETUNIQUE, 1);

   __edit = el_init(program, stdin, stdout, stderr);
   if (!__edit)
      printf("%s el_init()", __func__);

   el_set(__edit, EL_HIST, history, __hist);
   el_set(__edit, EL_EDITOR, EDITOR);
   el_set(__edit, EL_PROMPT, prompt);
}

rtsh::rtsh (const rtsh &_cls)
{
   *this = _cls;
}

rtsh &
rtsh::operator= (const rtsh &_cls)
{
   // __error = _cls.__error;
   memcpy(&__hist, &_cls.__hist, sizeof(__hist));
   memcpy(&__edit, &_cls.__edit, sizeof(__edit));
   __MaxHistory = 100;

   return *this;
}

rtsh::~rtsh()
{
   if (__edit) {
      el_reset(__edit);
      el_end(__edit);
   }

   if (__hist)
      history_end(__hist);
}

void
rtsh::console()
{
   bool flag = true;
   size_t length = 0;
   const char *buffer;

   while (flag) {
      buffer = el_gets(__edit, (int *)&length);
      printf("command is %s", buffer);

      if (strcmp(buffer, "exit") == 0)
         flag = false;
   }
}

}
